%define _KERNEL_MAC_EXTERN_OR_GLOBAL_ 1

%include"../include/gdt.mac"
%include"../include/idt.mac"
%include"../include/gate.mac"
%include"../include/kernel.mac"

[section .text]
main:
	;init gdt and idt 
	call init_gdt
	call init_idt_exception_trap

	;init pmm
	call pmm_init

[section .data]  
align 8 ;align on 8 bytes  
;default descriptor: 
;base:0x10000;limit:640KB; 
;ring 0 code segment  
gdt: 
	%rep MAX_GDT_DESC  
		istruc descriptor 
			at descriptor.limit_low,dw LIMIT_640KB_LOW 
			at descriptor.base_low,dw KERNEL_CODE_BASE_LOW  
			at descriptor.base_mid,db KERNEL_CODE_BASE_MID  
			at descriptor.type_attr,db 1001b<<4 | TYPE_E_R ;ring 0,execute,read  
			at descriptor.limit_attr,db 0101b<<4 | LIMIT_640KB_HIGH  
			at descriptor.base_high,db KERNEL_CODE_BASE_HIGH  
		iend 
	%endrep

;default is interrupt gate 
idt: 
	%rep MAX_IDT_DESC
		istruc gate_descriptor 
			at gate_descriptor.off_low,dw 0x0000  
			at gate_descriptor.selector,dw KERNEL_CODE_SEL 
			at gate_descriptor.attr,dw 1000b<<4|1110b ;interrupt gate  
			at gate_descriptor.off_high,dw 0x0000 
		iend 
	%endrep
